package com.suneast.suneaststore.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;
import com.suneast.suneaststore.util.storage.LattePreference;


public class AccountManager {

    private static boolean signState = false;
    private static String token = "";
    private static int level;//1.注册会员 2.经销商 3.体验中心 4.体验中心

    public enum SignTag {
        SIGN_STATE,
        SIGN_TOKEN,//临时token
        SIGN_AUTH, //永久token ,用于请求临时token
        SIGN_LEVEL //vip级别,后台用于标记分配相应权限
    }

    //在应用调起时,需调用 init()
    public static void init() {
        signState = LattePreference.getAppFlag(SignTag.SIGN_STATE.name());
        if (signState) {
            token = LattePreference.getCustomAppProfile(SignTag.SIGN_TOKEN.name());
            String levelStr = LattePreference.getCustomAppProfile(SignTag.SIGN_LEVEL.name());
            if (levelStr.length() > 0) {
                level = Integer.valueOf(levelStr);
            } else {
                level = 0;
            }
//            checkToken();
        }
    }

    private static void checkToken() {
        RestClient.builder()
                .url(API.CHECK_TOKEN)
                .params("sign", getSignFromLocal())
                .params("token", token)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject rootObj = JSON.parseObject(response);
                    if (rootObj.getIntValue("code") == 1) {//有新token
                        setTokenToLocal(rootObj.getJSONObject("data").getString("token"));
                        LatteLogger.e("error", "checkToken got new token");
                    } else {
                        LatteLogger.e("error", "checkToken token still new");
                    }
                })
                .error((code, msg) -> {
                    LatteLogger.e("error", "checkToken error code = "
                            + code + " msg = " + msg);
                })
                .failure(() -> {
                    LatteLogger.e("error", "checkToken failure");
                })
                .build()
                .post();
    }

    //在用户密码登录成功,验证码登录成功,注册成功 后将调用
    public static void update(boolean newState, int newLevel, String newToken, String newSign) {
        signState = newState;
        LattePreference.setAppFlag(SignTag.SIGN_STATE.name(), newState);
        level = newLevel;
        LattePreference.setCustomAppProfile(SignTag.SIGN_LEVEL.name(), String.valueOf(newLevel));
        token = newToken;
        LattePreference.setCustomAppProfile(SignTag.SIGN_TOKEN.name(), newToken);
        LattePreference.setCustomAppProfile(SignTag.SIGN_AUTH.name(), newSign);
    }

    public static boolean getSignState() {
        return signState;
    }

    public static int getLevel() {
        return level;
    }


    public static String getSignFromLocal() {
        return LattePreference.getCustomAppProfile(AccountManager.SignTag.SIGN_AUTH.name());
    }

    public static String getToken() {
        return token;
    }

    public static void setTokenToLocal(String newToken) {
        token = newToken;
        LattePreference.setCustomAppProfile(SignTag.SIGN_TOKEN.name(), newToken);
    }

    //用户退出登录后调用
    public static void exitCurrentAccount() {
        signState = false;
        level = 0;
        token = "";
        LattePreference.setAppFlag(SignTag.SIGN_STATE.name(), false);
        LattePreference.setCustomAppProfile(SignTag.SIGN_TOKEN.name(), "");
        LattePreference.setCustomAppProfile(SignTag.SIGN_AUTH.name(), "");
        LattePreference.setCustomAppProfile(SignTag.SIGN_LEVEL.name(), "");
    }
}
